package com.jbm.jbmsupplier.activity;

import android.Manifest;
import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.register.factoryuser.User_Register_Mode;
import com.jbm.jbmsupplier.permissions.PermissionsActivity;
import com.jbm.jbmsupplier.permissions.PermissionsChecker;
import com.jbm.jbmsupplier.presenters.LoginActivitys;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.views.LoginViews;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by 家帮忙 on 2017/4/10.
 * 李征
 */

public class LoginActivity extends AllActivityApp implements LoginViews {

    // 所需的全部权限
    static final String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_PHONE_STATE};//,Manifest.permission.WRITE_SETTINGS
    @InjectView(R.id.edname)
    TextInputEditText edname;
    @InjectView(R.id.ednames)
    TextInputLayout ednames;
    @InjectView(R.id.edpwd)
    TextInputEditText edpwd;
    @InjectView(R.id.edpwds)
    TextInputLayout edpwds;
    @InjectView(R.id.bu_login)
    Button buLogin;
    @InjectView(R.id.text_wjmm)
    TextView textWjmm;
    @InjectView(R.id.text_register)
    TextView textRegister;

    private PermissionsChecker mPermissionsChecker; // 权限检测器
    private static final int REQUEST_CODE = 0; // 请求码
    private DialogLoading dialogLoading;
    private LoginActivitys loginActivitys;

    @Override
    public int setContentView() {
        return R.layout.loginactivity_lz;
    }

    @Override
    public void initIalizeView() {
        ButterKnife.inject(this);
//        ExitActivity.getInstance().addActivity(this);
        loginActivitys = new LoginActivitys(this, this);
        mPermissionsChecker = new PermissionsChecker(this);
        loginActivitys.setInitLogin();//初始化数据

    }

    @Override
    protected void onResume() {
        super.onResume();
//        // 缺少权限时, 进入权限配置页面
        boolean boolead = mPermissionsChecker.lacksPermissions(PERMISSIONS);
        if (boolead) {
            startPermissionsActivity();
        }
    }

    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        // 拒绝时, 关闭页面, 缺少主要权限, 无法运行
        if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            finish();
        }

    }

    @Override
    public void getinitvies(boolean IsConnSu) {

    }

    @Override
    public void getLoginname(String name, String pwd) {//更新账号密码
        edname.setText(name);
        edpwd.setText(pwd);
    }

    @Override
    public void setError(String name) {//失败
        setDialogding();
        hint(name);
    }

    @Override
    public void setSucced() {//成功

        setDialogding();
        finish();
    }

    public void setDialogding() {
        if (dialogLoading != null) {
            dialogLoading.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginActivitys.setonDestroy();
    }

    long che=0;
    @Override
    public void onBackPressed() {

        if (System.currentTimeMillis() - che > 2000) {
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            che = System.currentTimeMillis();
        } else {
            ExitActivity.getInstance().exitActivitys();
            finish();

        }
//        exit("已注销用户是否退出？");
    }



    @OnClick({R.id.text_wjmm, R.id.text_register, R.id.bu_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bu_login:
                if (edname.getText().toString().equals("") ) {
//                    hint("请检查是否为空");
                    ednames.setError("请检查手机号是否为空");
                    return;
                } else if (edpwd.getText().toString().equals("")) {
//                    hint("请检查是否为空");
                    edpwds.setError("请检查密码是否为空");
                    return;
                } else if (edname.getText().toString().length() <11) {
//                    hint("请确认手机号");
                    ednames.setError("请确认手机号");
                    return;
                } else {
                    ednames.setErrorEnabled(false);
                    edpwds.setErrorEnabled(false);
                    loginActivitys.setLogin(edname.getText().toString(), edpwd.getText().toString());//获取用户名密码
                    dialogLoading = new DialogLoading(LoginActivity.this, "登录中.....", false);
                    dialogLoading.show();
                }
                break;
            case R.id.text_wjmm:
                break;
            case R.id.text_register:
                startActivity(new Intent(LoginActivity.this, User_Register_Mode.class));
                break;
        }
    }
}
